package bg.cinema_system.beans;

import java.sql.SQLException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import bg.cinema_system.pojo.UserTickets;
import bg.cinema_system.service.UserTicketsService;

@ManagedBean(name = "userTicketsList")
@RequestScoped
public class UserTicketsListBean {

	private int user_id;
	private String username;
	private List<UserTickets> userTickets;

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getUsername() throws SQLException {
		setUser_id();
		getUserTicketsList();

		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<UserTickets> getUserTickets() {
		return userTickets;
	}

	public void setUserTickets(List<UserTickets> userTickets) {
		this.userTickets = userTickets;
	}

	// Set the "user id", using the session parameter
	public void setUser_id() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(false);
		this.user_id = (Integer) session.getAttribute("userId");
	}

	// Get the username and his/her reserved tickets
	public void getUserTicketsList() throws SQLException {
		this.userTickets = UserTicketsService.getUserTicketsList(this.user_id);
		this.username = UserTicketsService.getUsername(this.user_id);
	}

}
